package com.flatlyapps.shoppinglist; 

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;

public class HttpHelper {
	static private String convertStreamToString(InputStream is) {
		//goes through each line of the reader, converts it, and then adds it to the StringBuilder
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		try{
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
		return sb.toString();
	}

	static public JSONArray loadJSON(String url) {
		HttpURLConnection connection = null;
		JSONArray json = null;
		InputStream is = null;
		try{
			//tries to get a connection within time limit
			connection = (HttpURLConnection) new URL(url).openConnection();
			connection.setConnectTimeout(5000);
			//gets the input stream from connection and makes a JSONArray from it
			is = new BufferedInputStream(connection.getInputStream());
			json = new JSONArray(convertStreamToString(is));
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}catch(JSONException je) {
			je.printStackTrace();
		} finally {
			//shutting down connection and input stream to avoid bugs
			try{
				if(is != null) {
					is.close();
				}
			}catch(IOException ioe) {
				ioe.printStackTrace();
			}
			if(connection != null) {
				connection.disconnect();
			}
		}
		return json;
	}

	static public InputStream loadImage(String url) {
		//setting input stream
		HttpURLConnection connection = null;
		InputStream is = null;
		try{
			connection = (HttpURLConnection) new URL(url).openConnection();
			connection.setConnectTimeout(15000);
			is = new BufferedInputStream(connection.getInputStream());
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
		return is;
	}
}
